home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Shaders / TexturingAndModeling:AProceduralApproach / KMShaders / KMShiny.sl < prev    next >
Encoding:
Text File  |  1995-03-22  |  1.9 KB  |  65 lines

  1. /*
  2.  * shiny.sl -- Shiny metal surface, using ray tracing.
  3.  *
  4.  * DESCRIPTION:
  5.  *   Makes a smoothly polished metal, using ray tracing to calculate
  6.  *   reflections of the environment.
  7.  * 
  8.  * PARAMETERS:
  9.  *    Ka, Kd, Ks, roughness, specularcolor - The usual meaning
  10.  *    Kr - coefficient for mirror-like reflections of environment
  11.  *    blur - how blurry are the reflections? (0 = perfectly sharp)
  12.  *    samples - set to higher than 1 for oversampling of blur
  13.  *
  14.  * AUTHOR: written by Larry Gritz, 1991
  15.  *
  16.  * HISTORY:
  17.  *      Aug 1991 -- written by lg in C
  18.  *      25 Jan 1994 -- recoded by lg in correct shading language.
  19.  *
  20.  * last modified 25 Jan 1994 by Larry Gritz
  21.  */
  22.  
  23.  
  24. surface
  25. KMShiny (float Ka = 1, Kd = 0.5, Ks = 1;
  26.      float Kr = 0.5, roughness = 0.005, blur = 0;
  27.      color specularcolor = 1;
  28.      float samples = 1; )
  29. {
  30.   point Nf;               /* Forward facing normal vector */
  31.   point IN;               /* normalized incident vector */
  32.   point Rdir;             /* Smooth reflection direction */
  33.   point uoffset, voffset; /* Offsets for blur */
  34.   color ev;               /* Color of the reflections */
  35.   point R;                /* Direction to cast the ray */
  36.   float i;
  37.  
  38.   /* Construct a forward facing surface normal */
  39.   Nf = faceforward (normalize(N), I);
  40.   IN = normalize (I);
  41.   ev = 0;
  42.  
  43.   /* Calculate the reflection color */
  44.   if (Kr > 0) {
  45.       /* Rdir gets the perfect reflection direction */
  46.       Rdir = normalize (reflect (IN, Nf));
  47.  
  48.       if (blur > 0) {
  49.       /* Construct orthogonal components to Rdir */
  50.       uoffset = normalize (point (zcomp(Rdir) - ycomp(Rdir),
  51.                       xcomp(Rdir) - zcomp(Rdir),
  52.                       ycomp(Rdir) - xcomp(Rdir)));
  53.       voffset = Rdir ^ uoffset;
  54.     }
  55.       R = Rdir;
  56.       ev = trace (P, R);
  57.       ev *= Kr / samples;
  58.     }
  59.  
  60.   /* The rest is like the plastic shader */
  61.   Oi = Os;
  62.   Ci = Os * ( Cs * (Ka*ambient() + Kd*diffuse(Nf)) +
  63.           specularcolor * (ev + Ks*specular(Nf,-IN,roughness)));
  64. }
  65.